package com.hailiao.beidou.message.ui.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.d10ng.compose.ui.show.Avatar
import com.hailiao.beidou.message.R

/**
 * 联系人头像
 * @Author d10ng
 * @Date 2023/11/22 11:46
 */
@Composable
fun ContactAvatar(
    name: String,
    size: Dp = 50.dp,
    fontSize: TextUnit = 22.sp,
) {
    val isCenter = remember(name) {
        name.contentEquals("指挥中心")
    }
    val char = remember(isCenter, name) {
        if (isCenter) ' ' else name.uppercase().firstOrNull() ?: ' '
    }
    Avatar(char = char, size = size, fontSize = fontSize) {
        Image(
            painter = painterResource(id = R.drawable.avatar_bg),
            contentDescription = null,
            modifier = Modifier.fillMaxSize()
        )
    }
}

@Preview
@Composable
private fun ContactAvatarTest() {
    Column {
        ContactAvatar(name = "张三")
        ContactAvatar(name = "李四")
        ContactAvatar(name = "指挥中心")
    }
}